/*
 *  cp.c
 *  
 *
 *  Created by Rayvionne French on 7/24/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "syscall.h"

int data;
int lock;
int cv;
int start;
int go;

void ConsumerProducer () {
	int value;
	
	LockAcquire(start);
	AlterIntMV(data, 0, 1);
	LockRelease(start);
	
	
	LockAcquire(lock);
	Write("CP waiting.\n", sizeof("CP waiting.\n"), ConsoleOutput);
	CVWait(cv, lock);
	Write("CP reading original value.\n", sizeof("PC writing orignal value.\n"), ConsoleOutput);
	value = RetrieveIntMV(data, 0);
	Write("CP writing new value.\n", sizeof("PC writing orignal value.\n"), ConsoleOutput);
	AlterIntMV(data, 0, 10);
	CVSignal(cv, lock);
	LockRelease(lock);
	
	Exit(value);
}
int main() {
	/* Initialize the shared data */
	data = CreateIntMV("data", 4, 1);
	
	lock = CreateLock("lock", 4);
	cv = CreateCV("cv", 2);
	
	go = CreateIntMV("go", 2, 1);
	start = CreateLock("start", 5);
	
	Fork(&ConsumerProducer, "ConsumerProducer", 16);
	
	Exit(-1);
}